package com.googlecode.skald.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A popup window the contains a scrolling VerticalPanel of contents 
 * and a close button to hide the popup.  
 * <p>
 * If size is not set, will be large enough to contain all content 
 * (without scrollbars), and so may take up more than the main window 
 * (but main window will scroll to handle it).  Otherwise, will only
 * have the given height in CSS units.  By default, starts at top,
 * but can modify this to scroll to bottom whenever shown.  
 * 
 * @author Zach Tomaszewski
 */
public class HidingPopup extends PopupPanel {

  private Widget contents;
  private ScrollPanel scroller;
  private boolean scrollToBottom = false;
  private String height = null;

  /** 
   * The given contents will be shown in this popup.
   * Full-size with view starting at top.
   */
  public HidingPopup(Widget contents) {
    this(contents, null, false);
  }
  
  /** The given contents will be shown in this popup. */
  public HidingPopup(Widget contents, String height, 
                     boolean scrollToBottom) {
    super();
    this.contents = contents;
    this.height = height;
    this.scrollToBottom = scrollToBottom;
    this.scroller = new ScrollPanel(this.contents);
    VerticalPanel panel = new VerticalPanel();
    panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    panel.add(this.scroller);
    Button hide = new Button("Hide");
    hide.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        HidingPopup.this.hide();        
      }      
    });
    panel.add(hide);
    this.add(panel);
  }
  
  @Override
  public void show() {
    // scroll to bottom
    if (this.height != null) {
      this.scroller.setHeight(this.height);
    }
    super.show();
    if (this.scrollToBottom) {
      this.scroller.scrollToBottom();
    }
  }
}
